﻿using System;
using CH.FourBoards.LagerWalti.Persistence.DataAccess;
using CH.FourBoards.LagerWalti.Persistence.Entities;
using CH.FourBoards.LagerWalti.Persistence.Setup.FixedData;

namespace CH.FourBoards.LagerWalti.Business.Warehouse
{
    public class WarehouseManager
    {
        public void AdjustToStocktaking(Product product, int stockQuantity)
        {
            using (var pm = new PersistenceManager()) {

                // check stock on db
                var currentQuantity = pm.GetStock(product);
                var delta = stockQuantity - currentQuantity;
                if (delta == 0) {
                    return;
                }

                // add adjustment entry
                var trans = new Transaction {
                    Date = DateTime.Now,
                    ProductId = product.Id,
                    Quantity = delta,
                    TypeId = (int) NamedTransactionTypes.Stocktaking
                };
                pm.Insert(trans);
            }
        }

        public int GetStock(Product product)
        {
            using (var pm = new PersistenceManager()) {
                return pm.GetStock(product);
            }
        }
    }
}
